package com.adcc.loadsheet.monitor.entity;

import com.adcc.loadsheet.monitor.util.AlarmEnum;
import com.adcc.loadsheet.monitor.util.Constant;
import com.adcc.loadsheet.monitor.util.StringUtil;
import org.joda.time.DateTime;

import java.util.HashSet;
import java.util.Set;

/**
 * 航信接口监控信息
 */
public class AirInfoMonitor extends BaseEntity implements Cloneable {

    // 入TOPIC消息数
    private long enqueued;

    // 最新报文更新时间
    private DateTime updateTime;

    // 无报文接收时间
    private long noReceiveTime;

    // MQ状态
    private boolean mqStatus;

    // TOPIC状态
    private boolean topicStatus;

    // 航信状态
    private MonitorStatus status = MonitorStatus.ABNORMAL;

    // 异常状态码(0：正常，1：异常，状态机:00[mqStatus|topicStatus],例如11标识模块监控项均异常)
    private String errorCode = "00";

    // 异常原因：XXX IS DOWN,XXX IS DOWN
    private String errorReason = "";

    // 本模块异常的节点
    private Set<String> alarmContent = new HashSet<>();

    // 本模块正常的节点（用于判断告警确认后，部分节点正常后又异常需要重新告警）
    private Set<String> unAlarmContent = new HashSet<>();

    public long getEnqueued() {
        return enqueued;
    }

    public void setEnqueued(long enqueued) {
        this.enqueued = enqueued;
    }

    public DateTime getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(DateTime updateTime) {
        this.updateTime = updateTime;
    }

    public long getNoReceiveTime() {
        return noReceiveTime;
    }

    public void setNoReceiveTime(long noReceiveTime) {
        this.noReceiveTime = noReceiveTime;
    }

    public boolean isMqStatus() {
        return mqStatus;
    }

    public void setMqStatus(boolean mqStatus) {
        this.mqStatus = mqStatus;
    }

    public boolean isTopicStatus() {
        return topicStatus;
    }

    public void setTopicStatus(boolean topicStatus) {
        this.topicStatus = topicStatus;
    }

    public MonitorStatus getStatus() {
        return status;
    }

    public void setStatus(MonitorStatus status) {
        this.status = status;
    }

    public String getErrorCode() {
        return errorCode;
    }

    public void setErrorCode(String errorCode) {
        this.errorCode = errorCode;
    }

    public String getErrorReason() {
        return errorReason;
    }

    public void setErrorReason(String errorReason) {
        this.errorReason = errorReason;
    }

    public Set<String> getAlarmContent() {
        return alarmContent;
    }

    public void setAlarmContent(Set<String> alarmContent) {
        this.alarmContent = alarmContent;
    }

    public Set<String> getUnAlarmContent() {
        return unAlarmContent;
    }

    public void setUnAlarmContent(Set<String> unAlarmContent) {
        this.unAlarmContent = unAlarmContent;
    }

    @Override
    public String toXML() throws Exception {
        return null;
    }

    @Override
    public String toJSON() throws Exception {
        return null;
    }

    @Override
    public Object clone() throws CloneNotSupportedException {
        AirInfoMonitor airInfo = new AirInfoMonitor();
        airInfo.setEnqueued(enqueued);
        airInfo.setUpdateTime(updateTime);
        airInfo.setNoReceiveTime(noReceiveTime);
        airInfo.setTopicStatus(topicStatus);
        airInfo.setStatus(status);
        airInfo.setErrorCode(errorCode);
        airInfo.setErrorReason(errorReason);
        airInfo.setAlarmContent(alarmContent);
        airInfo.setUnAlarmContent(unAlarmContent);
        return airInfo;
    }

    /**
     * 异常状态码
     */
    public void buildErrorCode() {
        StringBuilder sb = new StringBuilder();
        char[] states = new char[2];
        states[0] = mqStatus ? '0' : '1';
        states[1] = topicStatus ? '0' : '1';
        for(char c : states){
            sb.append(c);
        }
        this.errorCode = sb.toString();

        // [2024-6-01：张丽娟：增加声音和弹窗告警的功能，系统支持对指定告警项进行声音和弹窗告警忽略的功能: 根据状态码获取正常节点和异常节点信息]
        // 本模块异常的节点
        Set<String> alarmContent = new HashSet<>();
        // 本模块正常的节点（用于判断告警确认后，部分节点正常后又异常需要重新告警）
        Set<String> unAlarmContent = new HashSet<>();
        // 保存到数据库的异常信息
        StringBuilder errorReasonBuilder = new StringBuilder();
        AlarmEnum enumMQ = AlarmEnum.AIRINFO_MQ;
        if (states[0] == '1') {
            errorReasonBuilder.append(String.format(Constant.LOG_DOWN, enumMQ.getMysqlLogName()));
            alarmContent.add(enumMQ.getItem());
        }else{
            unAlarmContent.add(enumMQ.getItem());
        }

        AlarmEnum enumMsg = AlarmEnum.AIRINFO_MSG;
        if (states[1] == '1') {
            errorReasonBuilder.append(String.format(Constant.LOG_DOWN, enumMsg.getMysqlLogName()));
            alarmContent.add(enumMsg.getItem());
        }else{
            unAlarmContent.add(enumMsg.getItem());
        }
        this.alarmContent = alarmContent;
        this.unAlarmContent = unAlarmContent;
        this.errorReason = StringUtil.stringBuilderToString(errorReasonBuilder);
    }
}
